"""
简单K线形态判断 （日本蜡烛图里的）:

是否是长上影线 （high-close）/MAX(abs(open-close) ,一跳）>3  and （high-close）>2倍平均ATR（就是归一化处理要用的那个ATR值）
是否有长下影线 （close-low）/MAX(abs(open-close) >3 ,1跳)  and （close-low）>2倍平均ATR
是否大阳包大阴：(CLOSE-REF(CLOSE,1))>=2倍平均ATR   AND （REF(OPEN,1）-REF(CLOSE,1))>=2倍平均ATR AND CLOSE>REF(OPEN,1)
是否大阴包大阳：
(REF(CLOSE,1)-CLOSE)>=2倍平均ATR   AND （REF(CLOSE,1)-REF(OPEN,1）)>=2倍平均ATR AND CLOSE<REF(OPEN,1)
是否启明之星：(CLOSE-REF(CLOSE,1))>=2倍平均ATR   AND （REF(OPEN,2）-REF(CLOSE,2))>=2倍平均ATR AND CLOSE>REF(OPEN,2) and ABS(REF(CLOSE,1)-REF(OPEN,1))<=3跳
是否黄昏之星:   (REF(CLOSE,1)-CLOSE)>=2倍平均ATR   AND （REF(CLOSE,2)-REF(OPEN,2）)>=2倍平均ATR AND CLOSE<REF(OPEN,2)   and ABS(REF(CLOSE,1)-REF(OPEN,1))<=3跳
"""
import pandas as pd
import rqdatac as rq
from datetime import timedelta
from ..utility import make_domaint_time_dict, price_tick_map


def self_get_factor_df(sec_id, price_tick):
    symbol_time_dict = make_domaint_time_dict(sec_id)
    result_df = {}
    for symbol, time_dict in symbol_time_dict.items():
        prices = rq.get_price(symbol, start_date="20220801", end_date="20231116", frequency="1m",
                              fields=["open", "close", "high", "low"], expect_df=False)
        prices.index -= timedelta(minutes=1)
        atr = (prices["high"] - prices["low"]).rolling(1000).mean()
        oc = (prices["open"] - prices["close"]).abs()
        oc[oc < price_tick] = price_tick
        hc = prices["high"] - prices["close"]
        c11 = hc / oc > 3
        c12 = hc > 2 * atr
        prices["c1"] = 0
        prices.loc[c11 & c12, "c1"] = 1

        cl = prices["close"] - prices["low"]
        c21 = cl / oc > 3
        c22 = cl > 2 * atr
        prices["c2"] = 0
        prices.loc[c21 & c22, "c2"] = 1

        c31 = (prices["close"] - prices["close"].shift(1)) >= 2 * atr
        c32 = (prices["open"] - prices["close"]).shift(1) >= 2 * atr
        c33 = prices["close"] > prices["open"].shift(1)
        prices["c3"] = 0
        prices.loc[c31 & c32 & c33, "c3"] = 1

        c41 = (prices["close"].shift(1) - prices["close"]) >= 2 * atr
        c42 = (prices["close"] - prices["open"]).shift(1) >= 2 * atr
        c43 = prices["close"] > prices["open"].shift(1)
        prices["c4"] = 0
        prices.loc[c41 & c42 & c43, "c4"] = 1

        c51 = (prices["close"] - prices["close"].shift(1)) >= 2 * atr
        c52 = (prices["open"] - prices["close"]).shift(2) >= 2 * atr
        c53 = prices["close"] > prices["open"].shift(2)
        c54 = (prices["close"] - prices["open"]).shift(1).abs() <= 3 * price_tick
        prices["c5"] = 0
        prices.loc[c51 & c52 & c53 & c54, "c5"] = 1

        c61 = (prices["close"].shift(1) - prices["close"]) >= 2 * atr
        c62 = (prices["close"] - prices["open"]).shift(2) >= 2 * atr
        c63 = prices["close"] < prices["open"].shift(2)
        c64 = (prices["close"] - prices["open"]).shift(1).abs() <= 3 * price_tick
        prices["c6"] = 0
        prices.loc[c61 & c62 & c63 & c64, "c6"] = 1

        prices_df = prices[time_dict["start"]: time_dict["last"]]

        prices_df["symbol"] = symbol
        result_df[symbol] = prices_df
    return pd.concat(list(result_df.values()))


def get_factor_df(sec_id, prices):
    print(sec_id, __file__)
    price_tick = price_tick_map.get(sec_id.lower())
    if not price_tick:
        price_tick = price_tick_map.get(sec_id.upper(), 1)

    atr = (prices["high"] - prices["low"]).rolling(1000).mean()
    oc = (prices["open"] - prices["close"]).abs()
    oc[oc < price_tick] = price_tick
    hc = prices["high"] - prices["close"]
    c11 = hc / oc > 3
    c12 = hc > 2 * atr
    prices["c1"] = 0
    prices.loc[c11 & c12, "c1"] = 1

    cl = prices["close"] - prices["low"]
    c21 = cl / oc > 3
    c22 = cl > 2 * atr
    prices["c2"] = 0
    prices.loc[c21 & c22, "c2"] = 1

    c31 = (prices["close"] - prices["close"].shift(1)) >= 2 * atr
    c32 = (prices["open"] - prices["close"]).shift(1) >= 2 * atr
    c33 = prices["close"] > prices["open"].shift(1)
    prices["c3"] = 0
    prices.loc[c31 & c32 & c33, "c3"] = 1

    c41 = (prices["close"].shift(1) - prices["close"]) >= 2 * atr
    c42 = (prices["close"] - prices["open"]).shift(1) >= 2 * atr
    c43 = prices["close"] > prices["open"].shift(1)
    prices["c4"] = 0
    prices.loc[c41 & c42 & c43, "c4"] = 1

    c51 = (prices["close"] - prices["close"].shift(1)) >= 2 * atr
    c52 = (prices["open"] - prices["close"]).shift(2) >= 2 * atr
    c53 = prices["close"] > prices["open"].shift(2)
    c54 = (prices["close"] - prices["open"]).shift(1).abs() <= 3 * price_tick
    prices["c5"] = 0
    prices.loc[c51 & c52 & c53 & c54, "c5"] = 1

    c61 = (prices["close"].shift(1) - prices["close"]) >= 2 * atr
    c62 = (prices["close"] - prices["open"]).shift(2) >= 2 * atr
    c63 = prices["close"] < prices["open"].shift(2)
    c64 = (prices["close"] - prices["open"]).shift(1).abs() <= 3 * price_tick
    prices["c6"] = 0
    prices.loc[c61 & c62 & c63 & c64, "c6"] = 1

    return prices
